<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="人脸库ID" prop="faceSetId">
        <el-input
          v-model="queryParams.faceSetId"
          placeholder="请输入人脸库ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人脸库名称" prop="faceSetName">
        <el-input
          v-model="queryParams.faceSetName"
          placeholder="请输入人脸库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人脸库当前数量" prop="faceNumber">
        <el-input
          v-model="queryParams.faceNumber"
          placeholder="请输入人脸库当前数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人脸库最大数量" prop="faceSetCapacity">
        <el-input
          v-model="queryParams.faceSetCapacity"
          placeholder="请输入人脸库最大数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="自定义字段" prop="externalFields">
        <el-input
          v-model="queryParams.externalFields"
          placeholder="请输入自定义字段"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:FaceSetInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
         
         
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:FaceSetInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:FaceSetInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:FaceSetInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="FaceSetInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="Id" align="center" prop="id" />
      <el-table-column label="人脸库ID" align="center" prop="faceSetId" />
      <el-table-column label="人脸库名称" align="center" prop="faceSetName" />
      <el-table-column label="人脸库当前数量" align="center" prop="faceNumber" />
      <el-table-column label="人脸库最大数量" align="center" prop="faceSetCapacity" />
      <el-table-column label="自定义字段" align="center" prop="externalFields" />
        <el-table-column label="维护人" align="center" prop="createBy" />
          <el-table-column label="部门名称" align="center" prop="deptName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
           <el-button
          type="text"
          
         icon="el-icon-download"
          size="mini"
         
          @click="handleXiaFa(scope.row)"
          v-hasPermi="['system:FaceSetFace:edit']"
        >下发</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:FaceSetInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:FaceSetInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改系统人脸库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="人脸库ID" prop="faceSetId">
          <el-input v-model="form.faceSetId" placeholder="请输入人脸库ID" />
        </el-form-item>
        <el-form-item label="人脸库名称" prop="faceSetName">
          <el-input v-model="form.faceSetName" placeholder="请输入人脸库名称" />
        </el-form-item>
        <el-form-item label="人脸库当前数量" prop="faceNumber">
          <el-input v-model="form.faceNumber" placeholder="请输入人脸库当前数量" />
        </el-form-item>
        <el-form-item label="人脸库最大数量" prop="faceSetCapacity">
          <el-input v-model="form.faceSetCapacity" placeholder="请输入人脸库最大数量" />
        </el-form-item>
        <el-form-item label="自定义字段" prop="externalFields">
          <el-input v-model="form.externalFields" placeholder="请输入自定义字段" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createDate">
          <el-date-picker clearable
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
      <!-- 下发对话框 -->
<el-dialog 
  :title="title" 
  :visible.sync="xiafa" 
  width="500px" 
  append-to-body
  class="custom-dialog"
  :close-on-click-modal="false"
  :show-close="false"
 :center="true">
  <div class="dialog-content">
    <el-select 
      v-model="value" 
      placeholder="请选择下发选项"
      class="full-width-select"
      filterable
      clearable>
      <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>
    
    <div class="dialog-tips" v-if="options.length === 0">
      <i class="el-icon-warning"></i> 暂无可选选项
    </div>
  </div>
  
  <div slot="footer" class="dialog-footer">
    <el-button @click="cancel" class="cancel-btn">取 消</el-button>
    <el-button 
      type="primary" 
      @click="xiafaSubmit" 
      class="confirm-btn"
      :disabled="!value">
      确 定
    </el-button>
  </div>
</el-dialog>
  </div>
</template>

<script>
import { listFaceSetInfo, getFaceSetInfo, delFaceSetInfo, addFaceSetInfo, updateFaceSetInfo } from "@/api/system/FaceSetInfo";

export default {
  name: "FaceSetInfo",
  data() {
    return {
        options: [{
        value: '选项1',
        label: '前置机'
      }, {
        value: '选项2',
        label: '后置机'
      }, {
        value: '选项3',
        label: '人脸识别'
      }, {
        value: '选项4',
        label: '挂号机'
      }],
        value: '',
        xiafa: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 系统人脸库表格数据
      FaceSetInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        faceSetId: null,
        faceSetName: null,
        faceNumber: null,
        faceSetCapacity: null,
        externalFields: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询系统人脸库列表 */
    getList() {
      this.loading = true;
      listFaceSetInfo(this.queryParams).then(response => {
        this.FaceSetInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        faceSetId: null,
        faceSetName: null,
        faceNumber: null,
        faceSetCapacity: null,
        externalFields: null,
        createDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加系统人脸库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFaceSetInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改系统人脸库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFaceSetInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFaceSetInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除系统人脸库编号为"' + ids + '"的数据项？').then(function() {
        return delFaceSetInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/FaceSetInfo/export', {
        ...this.queryParams
      }, `FaceSetInfo_${new Date().getTime()}.xlsx`)
    },
       handleSearchFaceChange(file, fileList) {
        // 当文件选择发生变化时触发
    console.log('当前选择的文件:', file);
    // 将原始文件对象保存到form中，注意这里要用file.raw获取原始文件
       this.form.face = file;
      // this.test =JSON.parse(JSON.stringify(file.raw))
      // this.form.face = file;
      this.test = file.raw
      // this.test.push(file.raw);
    },
      /** 提交按钮 
    async submitForm() {
          // 检查是否已选择文件
    if (!this.form.face) {
        this.$message.error('请先选择要上传的人脸图片');
        return;
    }
    try {
      const a = {
              file:this.test
              // faceSetFace:
              //   {
              //   faceSetId:'2'
              // }
            }
            console.log(a);
            // return
        // 调用API上传
        const res = await  addFaceSetFace({file:this.test,faceSetId:this.form.faceSetId});
        console.log('上传成功:', res);
        this.$message.success('人脸添加成功');
         this.open = false;
              this.getList();
        // 可以在这里添加成功后的处理逻辑
    } catch (error) {
        console.error('上传失败:', error);
        this.$message.error('人脸添加失败');
    }
    },
    */
     handleXiaFa(row) {
      this.xiafa = true;
      this.title = "下发";
    },
     xiafaSubmit() {
      this.$modal.msgSuccess("下发成功");
      // this.reset();
      this.xiafa = false;
      this.value = ''
    },

  }
};
</script>
<style scoped>
.custom-dialog {
  border-radius: 8px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
}

.custom-dialog /deep/ .el-dialog__header {
  border-bottom: 1px solid #f0f0f0;
  padding: 15px 20px;
}

.custom-dialog /deep/ .el-dialog__title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.dialog-content {
  padding: 20px;
}

.full-width-select {
  width: 100%;
}

.dialog-tips {
  margin-top: 10px;
  color: #909399;
  font-size: 14px;
}

.dialog-tips i {
  margin-right: 5px;
}

.dialog-footer {
  text-align: center;
  padding: 15px 20px;
  border-top: 1px solid #f0f0f0;
}

.cancel-btn {
  width: 100px;
}

.confirm-btn {
  width: 100px;
  background-color: #409EFF;
  border-color: #409EFF;
}

.confirm-btn:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

.confirm-btn:disabled {
  background-color: #a0cfff;
  border-color: #a0cfff;
  cursor: not-allowed;
}

</style>